package com.example.shoppingdemo.nslcommodity.IResult;

import com.example.shoppingdemo.BaseApplication;
import com.example.shoppingdemo.nslcommodity.closebean.CloseDataResult;
import com.example.shoppingdemo.nslcommodity.closebean.CloseListResult;
import com.example.shoppingdemo.nslcommodity.closebean.CloseResult;
import com.example.shoppingdemo.nslcommodity.invbean.InvDataResult;
import com.example.shoppingdemo.nslcommodity.invbean.InvListResult;
import com.example.shoppingdemo.nslcommodity.invbean.InvResult;
import com.example.shoppingdemo.nslcommodity.javabean.AdapterModel;
import com.example.shoppingdemo.nslcommodity.conbean.ComDataResult;
import com.example.shoppingdemo.nslcommodity.conbean.ComListResult;
import com.example.shoppingdemo.nslcommodity.conbean.ComResult;
import com.example.shoppingdemo.nslcommodity.estbean.EstDataResult;
import com.example.shoppingdemo.nslcommodity.estbean.EstListResult;
import com.example.shoppingdemo.nslcommodity.estbean.EstResult;
import com.example.shoppingdemo.nslcommodity.javabean.CloseModel;
import com.example.shoppingdemo.nslcommodity.javabean.ComModel;
import com.example.shoppingdemo.nslcommodity.javabean.CommodityListModel;
import com.example.shoppingdemo.nslcommodity.javabean.EstModel;
import com.example.shoppingdemo.nslcommodity.javabean.InvModel;
import com.example.shoppingdemo.nslcommodity.javabean.PayModel;
import com.example.shoppingdemo.nslcommodity.javabean.RecModel;
import com.example.shoppingdemo.nslcommodity.javabean.ShopModel;
import com.example.shoppingdemo.nslcommodity.paybean.PayDataResult;
import com.example.shoppingdemo.nslcommodity.paybean.PayListResult;
import com.example.shoppingdemo.nslcommodity.paybean.PayResult;
import com.example.shoppingdemo.nslcommodity.recbean.RecDataResult;
import com.example.shoppingdemo.nslcommodity.recbean.RecListResult;
import com.example.shoppingdemo.nslcommodity.recbean.RecResult;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CommodityID implements ICommodity{
    ComModel comModel;

    RecModel recModel;

    EstModel estModel;

    PayModel payModel;

    CloseModel closeModel;

    InvModel invModel;
    @Override
    public void onComlistresult(int id,onComlistsResponse comlist) {
        List<ComModel> comModels=new ArrayList<>();

        BaseApplication.ishoppingRequest.onComlistResult(id)
                .map(new Func1<ComResult, ComDataResult>() {
                    @Override
                    public ComDataResult call(ComResult comResult) {
                        return comResult.getData();
                    }
                }).flatMap(new Func1<ComDataResult, Observable<ComListResult>>() {
            @Override
            public Observable<ComListResult> call(ComDataResult comDataResult) {
                return Observable.from(comDataResult.getList());
            }
        }).map(new Func1<ComListResult, ComModel>() {
            @Override
            public ComModel call(ComListResult comListResult) {
                List<ShopModel> shopModelList=new ArrayList<>();
                comModel=new ComModel();
                comModel.setId(comListResult.getId());
                comModel.setType(comListResult.getStatus());
                comModel.setSmoney(comListResult.getTotalAmount());
                comModel.setNumder(comListResult.getOrderSn());
                for (int i = 0; i <comListResult.getOrderItemList().length ; i++) {
                    ShopModel shopModel=new ShopModel();
                    shopModel.setIcon(comListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(comListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(comListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(comListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(comListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                comModel.setShopModelList(shopModelList);
                return comModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ComModel>() {
                    @Override
                    public void onCompleted() {
                        comlist.onComlist(comModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(ComModel comModel1) {
                        comModels.add(comModel1);
                    }
                });
    }

    @Override
    public void onEstlistresult(int id,onEstlistsResponse estlist) {
        List<EstModel> estModels=new ArrayList<>();

        BaseApplication.ishoppingRequest.onEstlistResult(id)
                .map(new Func1<EstResult, EstDataResult>() {
                    @Override
                    public EstDataResult call(EstResult estResult) {
                        return estResult.getData();
                    }
                }).flatMap(new Func1<EstDataResult, Observable<EstListResult>>() {
            @Override
            public Observable<EstListResult> call(EstDataResult estDataResult) {
                return Observable.from(estDataResult.getList());
            }
        }).map(new Func1<EstListResult, EstModel>() {
            @Override
            public EstModel call(EstListResult estListResult) {
                List<ShopModel> shopModelList=new ArrayList<>();
                estModel=new EstModel();
                estModel.setId(estListResult.getId());
                estModel.setSmoney(estListResult.getTotalAmount());
                estModel.setNumder(estListResult.getOrderSn());
                for (int i = 0; i <estListResult.getOrderItemList().length ; i++) {
                    ShopModel shopModel=new ShopModel();
                    shopModel.setIcon(estListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(estListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(estListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(estListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(estListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                estModel.setShopModelList(shopModelList);
                return estModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<EstModel>() {
                    @Override
                    public void onCompleted() {
                        estlist.onEstlist(estModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(EstModel estModel1) {
                        estModels.add(estModel1);
                    }
                });
    }

    @Override
    public void onPaylistresult(int id,onPaylistsResponse paylist) {
        List<PayModel> payModels=new ArrayList<>();

        BaseApplication.ishoppingRequest.onPaylistResult(id)
                .map(new Func1<PayResult, PayDataResult>() {
                    @Override
                    public PayDataResult call(PayResult payResult) {
                        return payResult.getData();
                    }
                }).flatMap(new Func1<PayDataResult, Observable<PayListResult>>() {
            @Override
            public Observable<PayListResult> call(PayDataResult payDataResult) {
                return Observable.from(payDataResult.getList());
            }
        }).map(new Func1<PayListResult, PayModel>() {
            @Override
            public PayModel call(PayListResult payListResult) {
                List<ShopModel> shopModelList=new ArrayList<>();
                payModel=new PayModel();
                payModel.setId(payListResult.getId());
                payModel.setSmoney(payListResult.getTotalAmount());
                payModel.setNumder(payListResult.getOrderSn());
                for (int i = 0; i <payListResult.getOrderItemList().length ; i++) {
                    ShopModel shopModel=new ShopModel();
                    shopModel.setIcon(payListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(payListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(payListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(payListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(payListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                payModel.setShopModelList(shopModelList);
                return payModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PayModel>() {
                    @Override
                    public void onCompleted() {
                        paylist.onPaylist(payModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(PayModel payModel1) {
                        payModels.add(payModel1);
                    }
                });
    }

    @Override
    public void onReclistresult(int id,onReclistsResponse reclist) {
        List<RecModel> recModels =new ArrayList<>();
        BaseApplication.ishoppingRequest.onReclistResult(id)
                .map(new Func1<RecResult, RecDataResult>() {
                    @Override
                    public RecDataResult call(RecResult recResult) {
                        return recResult.getData();
                    }
                }).flatMap(new Func1<RecDataResult, Observable<RecListResult>>() {
            @Override
            public Observable<RecListResult> call(RecDataResult recDataResult) {
                return Observable.from(recDataResult.getList());
            }
        }).map(new Func1<RecListResult, RecModel>() {
            @Override
            public RecModel call(RecListResult recListResult) {
                List<ShopModel> shopModelList=new ArrayList<>();
                recModel=new RecModel();
                recModel.setId(recListResult.getId());
                recModel.setSmoney(recListResult.getTotalAmount());
                recModel.setNumder(recListResult.getOrderSn());
                for (int i = 0; i <recListResult.getOrderItemList().length ; i++) {
                    ShopModel shopModel=new ShopModel();
                    shopModel.setIcon(recListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(recListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(recListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(recListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(recListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                recModel.setShopModelList(shopModelList);
                return recModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RecModel>() {
                    @Override
                    public void onCompleted() {
                        reclist.onReclist(recModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(RecModel recModel1) {
                        recModels.add(recModel1);
                    }
                });
    }

    @Override
    public void onCloselistresult(int id,CommodityID.onCloselistsResponse closelist) {
        List<CloseModel> closeModels =new ArrayList<>();
        BaseApplication.ishoppingRequest.onCloselistResult(id)
                .map(new Func1<CloseResult, CloseDataResult>() {
                    @Override
                    public CloseDataResult call(CloseResult closeResult) {
                        return closeResult.getData();
                    }
                }).flatMap(new Func1<CloseDataResult, Observable<CloseListResult>>() {
            @Override
            public Observable<CloseListResult> call(CloseDataResult closeDataResult) {
                return Observable.from(closeDataResult.getList());
            }
        }).map(new Func1<CloseListResult, CloseModel>() {
            @Override
            public CloseModel call(CloseListResult closeListResult) {
                List<ShopModel> shopModelList=new ArrayList<>();
                closeModel=new CloseModel();
                closeModel.setId(closeListResult.getId());
                closeModel.setSmoney(closeListResult.getTotalAmount());
                closeModel.setNumder(closeListResult.getOrderSn());
                for (int i = 0; i <closeListResult.getOrderItemList().length ; i++) {
                    ShopModel shopModel=new ShopModel();
                    shopModel.setIcon(closeListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(closeListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(closeListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(closeListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(closeListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                closeModel.setShopModelList(shopModelList);
                return closeModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CloseModel>() {
                    @Override
                    public void onCompleted() {
                        closelist.onCloselist(closeModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CloseModel closeModel1) {
                        closeModels.add(closeModel1);
                    }
                });
    }

    @Override
    public void onInvlistresult(int id,CommodityID.onInvlistsResponse invlist) {
        List<InvModel> invModels =new ArrayList<>();
        BaseApplication.ishoppingRequest.onInvlistResult(id)
                .map(new Func1<InvResult, InvDataResult>() {
                    @Override
                    public InvDataResult call(InvResult invResult) {
                        return invResult.getData();
                    }
                }).flatMap(new Func1<InvDataResult, Observable<InvListResult>>() {
            @Override
            public Observable<InvListResult> call(InvDataResult invDataResult) {
                return Observable.from(invDataResult.getList());
            }
        }).map(new Func1<InvListResult, InvModel>() {
            @Override
            public InvModel call(InvListResult invListResult) {
                List<ShopModel> shopModelList=new ArrayList<>();
                invModel=new InvModel();
                invModel.setId(invListResult.getId());
                invModel.setSmoney(invListResult.getTotalAmount());
                invModel.setNumder(invListResult.getOrderSn());
                for (int i = 0; i <invListResult.getOrderItemList().length ; i++) {
                    ShopModel shopModel=new ShopModel();
                    shopModel.setIcon(invListResult.getOrderItemList()[i].getProductPic());
                    shopModel.setMoney(invListResult.getOrderItemList()[i].getProductPrice());
                    shopModel.setNumder(invListResult.getOrderItemList()[i].getProductQuantity());
                    shopModel.setSku(invListResult.getOrderItemList()[i].getProductAttr());
                    shopModel.setTitle(invListResult.getOrderItemList()[i].getProductName());
                    shopModelList.add(shopModel);
                }
                invModel.setShopModelList(shopModelList);
                return invModel;
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<InvModel>() {
                    @Override
                    public void onCompleted() {
                        invlist.onInvlist(invModels);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(InvModel invModel1) {
                        invModels.add(invModel1);
                    }
                });
    }

    public interface onComlistsResponse
    {
        void onComlist(List<ComModel> comlists);
    }
    public interface onPaylistsResponse
    {
        void onPaylist(List<PayModel> paylists);
    }
    public interface onEstlistsResponse
    {
        void onEstlist(List<EstModel> estlists);
    }
    public interface onReclistsResponse
    {
        void onReclist(List<RecModel> reclists);
    }
    public interface onCloselistsResponse
    {
        void onCloselist(List<CloseModel> closelists);
    }
    public interface onInvlistsResponse
    {
        void onInvlist(List<InvModel> invlists);
    }
}
